include ImagePath
class DesignsController < ApplicationController
  before_filter :check_editor_right,:except=>[:index,:show,:male,:female]
  # GET /designs
  # GET /designs.xml
  def index
    @designs = Design.paginate :conditions=>{:active=>1},:page => params[:page],:per_page =>24,:order=>"id desc"
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { 
        render :xml => @designs.to_xml
      }
    end
  end

  def male
    @categories = Category.find(:all)
    respond_to do |format|
      format.html # index.html.erb
      format.xml  {render :xml => @categories.to_xml(:skip_types => true,:dasherize => false,
          :include=>{:male_designs=>{:except => [:active,:created_at,:updated_at,
                :sort_order,:image_url,:sex,:detail_image_url3,:quantity,
                :detail_image_url1,:detail_image_url2,:design_id,:category_id,:recommend,:height,:width],
              :methods =>[:image_path,:thumb_image_path,:x,:y]}},
          :except => [:active,:created_at,:updated_at,:sort_order])}
    end
  end

  def female
    @categories = Category.find(:all)
    respond_to do |format|
      format.html # index.html.erb
      format.xml  {render :xml => @categories.to_xml(:skip_types => true,:dasherize => false,
          :include=>{:female_designs=>{:except =>[:active,:created_at,:updated_at,
                :sort_order,:image_url,:sex,:detail_image_url3,:quantity,
                :detail_image_url1,:detail_image_url2,:design_id,:category_id,:recommend,:height,:width],
              :methods =>[:image_path,:thumb_image_path]}},
          :except => [:active,:created_at,:updated_at,:sort_order])}
    end
  end


  # GET /designs/1
  # GET /designs/1.xml
  def show
    @design = Design.find(params[:id])

    respond_to do |format|
      format.html {render :layout=>false}# show.html.erb
      format.xml  { render :xml => @design.to_xml(:skip_types => true,:dasherize => false,
          :except => [:active,:created_at,:updated_at,:sort_order,:image_url,:sn],
          :methods =>[:image_path,:thumb_image_path] ) }
    end
  end

  # GET /designs/new
  # GET /designs/new.xml
  def new
    @design = Design.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @design }
    end
  end

  # GET /designs/1/edit
  def edit
    @design = Design.find(params[:id])
  end

  # POST /designs
  # POST /designs.xml
  def create
    @design = Design.new(params[:design])
    @design.categories_array = params[:category]
    respond_to do |format|
      if @design.save
        flash[:notice] = 'Design was successfully created.'
        format.html { redirect_to(designs_url) }
        format.xml  { render :xml => @design, :status => :created, :location => @design }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @design.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /designs/1
  # PUT /designs/1.xml
  def update
    @design = Design.find(params[:id])
    @design.categories_array = params[:category]
    respond_to do |format|
      if @design.update_attributes(params[:design])
        flash[:notice] = '更新成功.'
        format.html { redirect_to(designs_url) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @design.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /designs/1
  # DELETE /designs/1.xml
  def destroy
    @design = Design.find(params[:id])
    @design.destroy

    respond_to do |format|
      format.html { redirect_to(designs_url) }
      format.xml  { head :ok }
    end
  end

  #  def remove_bg
  #    count = 0
  #
  #    Design.find(:all).each do |d|
  #      d.remove_background(params[:g_size].to_i,params[:t_size].to_i)
  #      count+=1
  #
  #    end
  #    render :text=>"#{count}个记录受影响"
  #  end

#  def resize
#    count = 0
#    Design.find(:all).each do |d|
#      d.resize
#      count+=1
#    end
#    render :text=>"#{count}个记录受影响"
#  end

  def change_cate
    Design.find(:all).each do |d|
      d.categories.clear
      d.categories << [Category.find(1),Category.find(2)]
    end
  end

  def admin
    @designs = Design.paginate :conditions=>{:active=>0},:page => params[:page],:per_page =>24,:order=>"id desc"
  end

  def reactive
    Design.update(params[:design].keys,params[:design].values)
    redirect_to :action=>:admin
  end
end
